#include "canvas.h"
#include "ui_canvas.h"
#include "point3d.h"
#include <QPainter>

Canvas::Canvas(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Canvas)
{
    ui->setupUi(this);

    lastLeft = NULLPOINT;


    m_black.setColor(Qt::black);
    m_black.setStyle(Qt::SolidLine);
    m_black.setWidth(2);

    m_pointBlack.setColor(Qt::black);
    m_pointBlack.setStyle(Qt::SolidLine);
    m_pointBlack.setWidth(4);

    m_green.setColor(Qt::green);
    m_green.setStyle(Qt::SolidLine);
    m_green.setWidth(2);

    m_pointGreen.setColor(Qt::green);
    m_pointGreen.setStyle(Qt::SolidLine);
    m_pointGreen.setWidth(4);

    buffer = QImage(QSize(50,50), QImage::Format_RGB32);
    QPainter p;
    p.begin(&buffer);
    p.fillRect(buffer.rect(),Qt::white);
    p.end();
}

Canvas::~Canvas()
{
    delete ui;
}

void Canvas::mousePressEvent(QMouseEvent *e)
{
    lastLeft = ui->label->mapFromGlobal(e->globalPos());
    if(e->button() == Qt::LeftButton)
    {
        emit left(lastLeft);
        e->accept();
    }
    else
    {
        e->ignore();
    }
}

void Canvas::mouseReleaseEvent(QMouseEvent *e)
{
    lastLeft = NULLPOINT;
    if(e->button() == Qt::LeftButton)
    {
        emit leftRelease();
        e->accept();
    }
    else
    {
        e->ignore();
    }
}

void Canvas::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons() == Qt::LeftButton)
    {
        QPoint p = ui->label->mapFromGlobal(e->globalPos());
        QPainter painter;
        painter.begin(&buffer);
        painter.drawLine(lastLeft,p);
        lastLeft = p;
        painter.end();
        paintBuffer();

        emit left(lastLeft);
        e->accept();
    }else
        e->ignore();

}

void Canvas::resizeEvent(QResizeEvent *)
{
    setupBuffer();
}

void Canvas::setupBuffer()
{
    QImage nb(ui->label->size(), QImage::Format_RGB32);
    QPainter p;
    p.begin(&nb);
    p.fillRect(nb.rect(),Qt::white);
    p.drawImage(QPoint(0,0), buffer);
    p.end();

    buffer = nb;
    paintBuffer();
}

void Canvas::clear()
{
    QPainter p;
    p.begin(&buffer);
    p.fillRect(buffer.rect(),Qt::white);
    p.end();

    paintBuffer();
}

void Canvas::paintBuffer()
{
    ui->label->setPixmap(QPixmap::fromImage(buffer));
}

void Canvas::drawCurvePoligonal(const std::vector<Point3D>& points, bool original)
{
    QPen line;
    QPen point;

    if(original)
    {
        line = m_black;
        point = m_pointBlack;
    }else
    {
        line = m_green;
        point = m_pointGreen;
    }

    QPainter p;
    p.begin(&buffer);

    QPoint p1(points[0].x(), points[0].y());
    QPoint p2(points[points.size()-1].x(), points[points.size()-1].y());

    p.setPen(line);
    p.drawLine(p1,p2);
    p.setPen(point);
    p.drawPoint(p1);
    for(int i = 1; i < points.size(); ++i)
    {
        p1 = QPoint(points[i-1].x(), points[i-1].y());
        p2 = QPoint(points[i].x(), points[i].y());
        p.setPen(line);
        p.drawLine(p1,p2);
        p.setPen(point);
        p.drawPoint(p2);
    }
    p.end();
    paintBuffer();
}
